package com.example.mywanandroid.project;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;
import com.example.mywanandroid.R;
import com.example.mywanandroid.util.GlobalLoading;
import com.example.mywanandroid.project.business.GetData;
import com.example.mywanandroid.project.business.GetProjectRecyclerViewCallBack;
import com.example.mywanandroid.project.business.GetProjectTitleCallBack;
import com.example.mywanandroid.project.business.ParseData;
import java.util.ArrayList;
import java.util.List;

public class ProjectFragment extends Fragment {
    private List<Fragment> viewList;//Fragment合集
    private List<String> titleList;//title集合

    private View root;
    private ViewPager viewPager;
    private PagerTabStrip pagerTabStrip;
    private GlobalLoading globalLoading;
    private List<ProjectRecyclerViewItem> projectRecyclerViewItems;

    private MyFragmentPagerAdapter adapter;

    private final int UPDATE_RECYCLERVIEW = 1;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case UPDATE_RECYCLERVIEW:
                {
                    List<String> params = (List<String>) msg.obj;
                    String titleData = params.get(0);
                    String recyclerViewData = params.get(1);
                    globalLoading.showLoadUi(false, 0);
                    titleList = ParseData.parseProjectTitleData(titleData);
                    projectRecyclerViewItems = ParseData.parseProjectRecyclerViewData(recyclerViewData);
                    // 添加FragmentList内容
                    for(String title : titleList) {
                        viewList.add(new ChildFragment(projectRecyclerViewItems));
                    }
                    globalLoading.showLoadUi(true, 0);
                    // 配置Adapter要在回调函数里面，否则数据不一致，外部的titleList和viewList为空值
                    adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), viewList, titleList);
                    viewPager.setAdapter(adapter);
                    break;
                }
                default:
                    break;
            }
        }
    };
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle saveInstanceState) {
        Log.d("ProjectFragment", "onCreateView: ");
        root = inflater.inflate(R.layout.fragment_project, container, false);
        viewPager = root.findViewById(R.id.project_view_pager);
        viewList = new ArrayList<>();

        pagerTabStrip = root.findViewById(R.id.project_tab);
        pagerTabStrip.setTextColor(Color.BLACK);// 标题颜色，这里需要带透明度的颜色值
        pagerTabStrip.setTabIndicatorColor(Color.rgb(250, 128, 10));// 指示器颜色，这里需要带透明度的颜色值
        pagerTabStrip.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);// 字体大小
        globalLoading = root.findViewById(R.id.tel_login_loading_img);

        GetData.getProjectTitleData(new GetProjectTitleCallBack() {
            @Override
            public void onGetProjectTitleData(final String titleData) {
                titleList = ParseData.parseProjectTitleData(titleData);
                GetData.getProjectRecyclerViewData(1, new GetProjectRecyclerViewCallBack() {
                    @Override
                    public void onGetProjectRecyclerViewCallBack(final String recyclerViewData) {
                        Message msg = new Message();
                        msg.what = UPDATE_RECYCLERVIEW;
                        List<String> params = new ArrayList<>();
                        params.add(titleData);
                        params.add(recyclerViewData);
                        msg.obj = params;
                        handler.sendMessage(msg);
                    }
                });
            }
        });
        return root;
    }
}
